package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.db.*;
import com.itheima.mapper.*;
import com.itheima.service.db.TestSoulService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @Autor JiangYunTian
 * @Date 2022/11/10
 * @Function 测灵魂实现类
 */
@DubboService
public class TestSoulServiceImpl implements TestSoulService {
    @Autowired
    private QuestionUserLockMapper questionUserLockMapper;

    @Autowired
    private SurveyMapper surveyMapper;

    @Autowired
    private QuestionsMapper questionsMapper;

    @Autowired
    private OptionsMapper optionsMapper;

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    //根据用户id查询用户的问卷锁记录
    @Override
    public List<QuestionUserLock> findQuestionUserLockByUserId(Long userId) {

        //创建查询对象
        LambdaQueryWrapper<QuestionUserLock> qw = new LambdaQueryWrapper();
        qw.eq(QuestionUserLock::getUserId, userId);

        return questionUserLockMapper.selectList(qw);
    }

    //保存用户的问卷锁记录
    @Override
    public void saveQuestionUserLock(QuestionUserLock questionUserLock) {
        questionUserLockMapper.insert(questionUserLock);
    }

    //通过问卷id查询问卷属性
    @Override
    public List<Survey> findSurveyBySurveyIdList(List<Long> surveyIdList) {
        LambdaQueryWrapper<Survey> qw = new LambdaQueryWrapper();
        qw.in(Survey::getId, surveyIdList);

        return surveyMapper.selectList(qw);
    }

    //根据问卷id查询题目
    @Override
    public List<Questions> findQuestionsBySurveyId(Long surveyId) {
        LambdaQueryWrapper<Questions> qw = new LambdaQueryWrapper();
        qw.eq(Questions::getSurveyId, surveyId);

        return questionsMapper.selectList(qw);
    }

    //根据问题id查选项
    @Override
    public List<Options> findOptionsByQuestionsId(Long questionsId) {
        return optionsMapper.findByQuestionsId(questionsId);
    }

    //计算得分
    @Override
    public Integer calculateScore(List<Answers> answers) {

        //初始化得分
        Integer sum = 0;

        //遍历answers计算得分
        for (Answers answer : answers) {

            Long optionId = Long.parseLong(answer.getOptionId());

            //调用mapper查询该选项得分
            sum += optionsMapper.findByOptionsId(optionId).getScore();

        }

        return sum;
    }

    //得到问卷id
    @Override
    public Long findSurveyIdByAnswers(List<Answers> answers) {

        Answers answers1 = answers.get(1);
        Long questionId = Long.parseLong(answers1.getQuestionId());

        return questionsMapper.selectById(questionId).getSurveyId();

    }

    //通过userId和surveyId查询锁对象
    @Override
    public QuestionUserLock findQuestionUserLockById(Long userId, Long surveyId) {

        LambdaQueryWrapper<QuestionUserLock> qw = new LambdaQueryWrapper();

        qw.eq(QuestionUserLock::getUserId, userId);
        qw.eq(QuestionUserLock::getSurveyId, surveyId);

        return questionUserLockMapper.selectOne(qw);

    }

    //保存锁对象
    @Override
    public void updateQuestionUserLock(QuestionUserLock questionUserLock) {

        questionUserLockMapper.updateById(questionUserLock);

    }

    //保存或更新report对象
    @Override
    public Long saveOrUpdateReport(Report report) {
        Report report1 = reportMapper.selectById(report.getId());
        //若report1不为空,则更新
        if (report1 != null) {
            reportMapper.updateById(report);
        } else {
            //若为空,则添加
            reportMapper.insert(report);
        }

        return report.getId();
    }

    //查询report对象
    @Override
    public Report findReportById(Long reportId) {

        return reportMapper.selectById(reportId);
    }

    //查找分数与用户相似的用户
    @Override
    public List<UserInfo> findSimilarYou(Long surveyId, Integer score) {

        //先根据分数查找相似用户id
        List<Long> userIdList = reportMapper.findSimilarYou(surveyId, score);

        //初始化返回的list
        List<UserInfo> userInfoList = new ArrayList<>();

        for (Long userId : userIdList) {
            userInfoList.add(userInfoMapper.selectById(userId));
        }
        return userInfoList;
    }

    //根据用户id查报告
    @Override
    public Report findReportByUserIdAndSurveyId(Long userId,Long surveyId) {

        return reportMapper.findReportByUserId(userId,surveyId);
    }
}
